import 'dart:convert';

import 'package:get/get.dart';

import '../api/authApi.dart';
import '../enum/roles.dart';
import '../enum/token.dart';
import '../model/user_entity.dart';
import '../tools/commons.dart';

/**
 * 用户状态管理
 * @author 梁超映
 * */
class UserController extends GetxController{
  late Rx<UserEntity> user = UserEntity().obs;

  //退出登录
  logout() async {
    //重新赋值
    user.value = UserEntity();
    //清空token
    CommonUtils.clearToken(Token.AccessToken);
    CommonUtils.clearToken(Token.RefreshToken);
    update();
  }
  //更新用户信息
  updateUser() async {{
      //解析返回结果
      var res = await getCurrentUser();
      try{
        Map<String,dynamic> data = json.decode(res.toString())["data"];
        print("已登录");
        user = UserEntity.fromJson(data).obs;
      }catch(e){
        print("未登录");
        UserEntity notLoginUser= UserEntity();
        notLoginUser.userRole = Auth.not_Login.toString();
        user = notLoginUser.obs;
      }
      update();
    }
  }


}